package com.dgshanger.sanhxiaofeisc.ui.home.activity

import android.view.View
import android.widget.LinearLayout
import android.widget.SeekBar
import com.dgshanger.sanhxiaofeisc.R
import com.dgshanger.sanhxiaofeisc.entity.CodePaySettingEntity
import com.dgshanger.sanhxiaofeisc.mvp.contract.CodePaySettingContract
import com.dgshanger.sanhxiaofeisc.mvp.presenter.CodePaySettingPresenter
import com.xx.baseuilibrary.mvp.lcec.BaseMvpLcecActivity
import kotlinx.android.synthetic.main.activity_code_pay_setting.*

/**
 * Created by JacobHHH on 2018/4/22.
 * 二维码的付款方式设置
 */
class CodePaySettingActivity : BaseMvpLcecActivity<LinearLayout,CodePaySettingEntity,CodePaySettingContract.Model,CodePaySettingContract.View,CodePaySettingPresenter>(),CodePaySettingContract.View, View.OnClickListener {

    private lateinit var mPayType : String

    override fun createPresenter(): CodePaySettingPresenter = CodePaySettingPresenter()

    override fun getActivityLayoutId(): Int = R.layout.activity_code_pay_setting

    override fun initData() {
        super.initData()
        initView()
        loadData(true)
    }

    private fun initView() {
        ll_loose.setOnClickListener(this)
        ll_coin.setOnClickListener(this)
        ll_diy.setOnClickListener(this)
        seek_bar.progress = 50
        seek_bar.max=100

    }

    override fun initEvent() {
        seek_bar.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener{
            override fun onProgressChanged(p0: SeekBar?, p1: Int, p2: Boolean) {
                /*if (p1>=50){*/
                    tv_loose.text=p1.toString()+"%"
                    tv_coin.text=(100-p1).toString()+"%"
               /* }else if (p1<=50){
                    tv_loose.text=(100-p1).toString()+"%"
                    tv_coin.text=p1.toString()+"%"
                }*/
                seek_bar.progress = p1
            }

            override fun onStartTrackingTouch(p0: SeekBar?) {
                changeUI(3)
            }

            override fun onStopTrackingTouch(p0: SeekBar?) {
                saveStatus()
            }

        })
    }

    override fun onClick(view: View?) {
        /*if (isDoubleClick()){//防止快速点击*/
            when(view!!.id){
                R.id.ll_loose->{//零钱
                    changeUI(1)
                    saveStatus()
                }
                R.id.ll_coin->{//购物币
                    changeUI(2)
                    saveStatus()
                }
                R.id.ll_diy->{//自定义
                    changeUI(3)
                }
            }
        /*}*/
    }


    private fun changeUI(type : Int){
        when(type){
            1->{ //零钱
                iv_loose.visibility=View.VISIBLE
                iv_coin.visibility=View.GONE
                iv_diy.visibility=View.GONE
                mPayType="main_wallet"
            }
            2->{ //购物币
                iv_loose.visibility=View.GONE
                iv_coin.visibility=View.VISIBLE
                iv_diy.visibility=View.GONE
                mPayType="main_integral"
            }
            3->{ //自定义
                iv_loose.visibility=View.GONE
                iv_coin.visibility=View.GONE
                iv_diy.visibility=View.VISIBLE
                mPayType="main_custom"
            }
        }
    }

    override fun loadData(refresh: Boolean) {
        presenter.loadData(refresh)
    }

    private fun saveStatus(){
        val loose = tv_loose.text.toString().trim().substring(0, tv_loose.text.toString().trim().length - 1)
        val coin = tv_coin.text.toString().trim().substring(0, tv_coin.text.toString().trim().length - 1)
        presenter.saveCodePaySettingStatus(mPayType,loose,coin)
    }

    override fun setData(data: CodePaySettingEntity?) {
        showContent()
        changeUI(data!!.pay_type.toInt())

        tv_loose.text=data.pay_wallet_rate+"%"
        tv_coin.text=data.pay_integral_rate+"%"

        val loose = data.pay_wallet_rate.toDouble().toInt()
        seek_bar.progress=loose
    }

    /**
     * 防止连续点击
     */
    private var lastClickTime: Long = 0

    fun isDoubleClick(): Boolean {
        val currentTime = System.currentTimeMillis()//当前时间
        val timeInterval = currentTime - lastClickTime
        if (0 < timeInterval && timeInterval < 1500) {
            return false//如果间隔在0-1.秒内就是快速重复点击
        }
        lastClickTime = currentTime
        return true
    }

}